package filter;


import utils.JDBCUtils;

import javax.servlet.*;
import java.io.IOException;

/**
 * @author 飞哥
 * @version V1.0
 * @package filter
 * @date 2021/5/29 17:34
 */
public class TransactionFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        try {
            filterChain.doFilter(servletRequest,servletResponse);
            JDBCUtils.commitAndClose();// 提交事务
        } catch (Exception e) {
            JDBCUtils.rollbackAndClose();// 回滚事务
            e.printStackTrace();
            throw new RuntimeException(e);//把异常抛给tomcat管理展示有好的错误页面
            //在web.xml中配置
        }
    }

    @Override
    public void destroy() {

    }
}
